package com.jiazhong.mingxing.boot.boot11.延迟消息;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class 延迟消息生产 {

    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("jiazhong-e");
        producer.setNamesrvAddr("124.220.170.254:9876");
        producer.start();
        String topic = "TopicD";
        String tags = "yanchi";
        String body = "Hello,Yanchi!DateTime is " + getNowDate();
        Message message = new Message(topic, tags, body.getBytes());
        // 设置延迟时间
        message.setDelayTimeLevel(4);
        SendResult sendResult = producer.send(message);
        log.info("sendResult={}", sendResult);
        producer.shutdown();
    }

    private static String getNowDate() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
}
